#include "Roman_int.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

vector<Roman_arithmetic::Roman_int> read_romans_from_file(string fname);
void print_romans_to_file (const vector<Roman_arithmetic::Roman_int>&, string);



int main()
{
	try
	{
		string fname = "";
		cout << "enter in file name:\n";
		cin  >> fname;
		vector<Roman_arithmetic::Roman_int> rr =	
			read_romans_from_file(fname);

		cout << "enter out file name:\n";
		cin  >> fname;
		print_romans_to_file(rr, fname); 
	
		system("pause");
		return 0;
	}
	catch (exception& e)
	{
		cerr << e.what();
		system("pause");
		return 1;
	}
	catch (...)
	{
		cerr << "Unknown error";
		system("pause");
		return 2;
	}
}

vector<Roman_arithmetic::Roman_int> read_romans_from_file(string fname)
{
	vector<Roman_arithmetic::Roman_int> buffer;
	Roman_arithmetic::Roman_int r;
	int n;
	
	ifstream ifs(fname.c_str()); 
	if (!ifs) throw exception("Cannot read from file");
	while (ifs >> n >> r)
	{
		buffer.push_back(r);
	}
	
	return buffer;
}


void print_romans_to_file (const vector<Roman_arithmetic::Roman_int>& r, string fname)
{
	ofstream ofs(fname.c_str());
	if (!ofs) throw exception("Cannot write to file...");

	for (size_t i = 0; i < r.size(); ++i)
		ofs << r[i].get_value() << '\t' << r[i].get_value_roman() << endl; 

}